<?php
/**
 * Extends views/payment.
 * 
 * @author Klarna
 */
class klarna_payment extends klarna_payment_parent {
	
	/**
	 * Current class template name.
	 * @var string
	 */
	protected $_sThisTemplate = 'klarna_payment.tpl';
	private $kutil = null;
	
	/**
	 * Adds extra information that is used in the view.
	 * 
	 * @Override
	 * @return returns Klarnas edited version of the template file.
	 */
	public function render() {
		parent::render();
		
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}
		
		$this->_aViewData['kutil'] = $this->kutil;
		$this->_sPaymentError = $this->kutil->getPaymentErrorCode();
		$this->_sPaymentErrorText = $this->kutil->loadError();
		$this->_aViewData["hasPclasses"] = !klarnapclasses::isEmpty();
		$this->_aViewData["pclasses"] = $this->kutil->getPclasses('', '', $this->getUser());
		
		$myConfig  = $this->getConfig();
		$shopid = $myConfig->getShopId();
		$country = " AND country = " . $this->kutil->getCountryNumber($this->getUser());
		
		$oGroups = oxNew('oxlist');
		$oGroups->init('klarnapclasses');
		$oGroups->selectString( "SELECT * FROM klarnapclasses WHERE oxshopid = '" . $shopid . "'" . $country . " AND type = 2;");
		foreach($oGroups as $pclass) {
			$this->_aViewData["scpclass"] = $pclass;
		}
		
		$this->_aViewData["kconf"] = $this->kutil->getKlarnaConfig();
		
		$this->_aViewData["basketcost"] = $this->kutil->getRealBasketCost();
		
		$this->_aViewData["kiurl"] = $this->kutil->getImageUrl($this->getUser(), 'ki');
		$this->_aViewData["kppurl"] = $this->kutil->getImageUrl($this->getUser(), 'kpp');
		$this->_aViewData["kscurl"] = $this->kutil->getImageUrl($this->getUser(), 'ksc');
		
		return $this->_sThisTemplate;
	}
	
	/**
	 * Used to transform the birthdate stored in the DB, if a date is given then it will return
	 * with the same date, otherwise it will fetch it and convert it from the DB.
	 * 
	 * @Override
	 * @return returns the given date, or transform the stored date from the DB.
	 */
	public function transformDate($date) {
		if($date || $this->getUser()->oxuser__oxbirthdate->value == '0000-00-00') {
			return $date;
		}
		
		if(!$this->kutil) {
			$this->kutil = oxNew('klarnautil');
		}
		
		return $this->kutil->transformDate($this->getUser()->oxuser__oxbirthdate->value, $this->getUser());
	}
}
?>